package console;

import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author Michael Hanns
 *
 */
public class CommandLineClient implements Observer {

    private ClientShell shell;
    private ClientPreferences prefs;
    private IOBuffer terminal;

    public CommandLineClient(double softwareVersion) {
        prefs = new ClientPreferencesImp(true);
        terminal = new IOBufferImp();
        terminal.addObserver(this);
        shell = new ClientShellImp(terminal);
    }

    public void takeInput() {
        while (true) {
            System.out.print("\n>");
            sendToShell(System.console().readLine());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public void sendToShell(String input) {
        if (prefs.isLocalEcho()) {
            System.out.print("\n>" + input + "\n");
        }
        shell.inputLine(input);
    }

    @Override
    public void update(Observable target, Object o) {
        String text = ((IOBuffer) target).getText();
        System.out.print(text);
    }
}
